package com.titaniume.rqbbitmq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.titaniume.rqbbitmq.util.RabbitMqUtils;

import java.util.Scanner;

/**
 * @ProjectName: rqbbitmq-hello
 * @Package: com.titaniume.rqbbitmq.three
 * @ClassName: Task02
 * @Author: huangrenfei
 * @Description: 消息生产者
 * @Date: 2021/12/17 16:58
 * @Version: 1.0
 */
public class Task02 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //开启发布确认
        channel.confirmSelect();

        //声明队列
        //消息队列持久化
        boolean durable = true;
        channel.queueDeclare(TASK_QUEUE_NAME,durable,false,false,null);
        //控制台中输入消息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入消息");
        while (scanner.hasNext()) {
            String message = scanner.nextLine();

            //设置生产者发送消息为持久化消息 （要求保存到磁盘） 默认是保持到内存中
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            System.out.println("生产者发出消息:" + message);
        }
    }
}
